package RPPM.servlet;

import RPPM.dao.impl.TeaDaoImpl;
import RPPM.domain.Teacher;
import RPPM.domain.ResultInfo;
import RPPM.service.Impl.StuServiceImpl;
import RPPM.service.Impl.TeaServiceImpl;
import RPPM.service.StuService;
import RPPM.service.TeaService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@WebServlet("/teaservlet/*")
public class TeaServlet extends BaseServlet{
    public void teaMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("teaMenu");

        int tea_id = (int) request.getSession().getAttribute("teaid");
        System.out.println(tea_id);

        TeaService teaService = new TeaServiceImpl();

        Teacher tobeckecked = new Teacher();
        Teacher teacher;
        tobeckecked.setTea_id(tea_id);

        teacher = teaService.findTeacherById(tobeckecked);

        ResultInfo info = new ResultInfo();

        response.setContentType("text/html;charset=utf-8");
        String[] map = {teacher.getTea_name(), teacher.getTea_sex(), teacher.getTea_phone(), teacher.getTea_posts(), teacher.getTea_major()};

        for (String str: map)
            System.out.println(str);

        info.setData(map);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void teaCheckCourse(HttpServletRequest request, HttpServletResponse response) throws IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("teaCheckCourse");


        int tea_id = (int) request.getSession().getAttribute("teaid");
        System.out.println(tea_id);

        System.out.println("session" + tea_id);

        TeaService teaService = new TeaServiceImpl();
        List<Map<String, Object>> courses = teaService.findCourseByTeaid(new Teacher(tea_id));

        for(Map map: courses) {
            System.out.println(map);
            for (Object key:map.keySet()){
                System.out.println(map.get(key).getClass());
                if(key.equals("Cou_time")){
                    System.out.println("change");
                    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    Object o = map.get(key);
                    map.replace(key, df.format(o));
                    break;
                }
                System.out.println(map.get(key).getClass());
            }
        }

        System.out.println(courses);


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
    public void searchCourse(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        System.out.println("searchCourse");


        String searchText = request.getParameter("searchText");
        int tea_id = (int) request.getSession().getAttribute("teaid");
        System.out.println(searchText);

        TeaService teaService = new TeaServiceImpl();
        List<Map<String, Object>> courses = teaService.findCoursesByname(tea_id, searchText);

        for(Map map: courses)
            System.out.println(map);
        System.out.println(courses);


        ResultInfo info = new ResultInfo();
        info.setData(courses);
        try {
            writeJsonValue(response, info);
            System.out.println("json回写成功");
        } catch (IOException e) {
            System.out.println("json回写失败");
        }

    }
}
